//
//  NYRecommendManager.m
//  Nooryes
//
//  Created by baken on 2020/2/22.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYRecommendManager.h"
#import "NYTopicCategoryModel.h"
#import "NYRecommendSlideModel.h"
#import "NYArticleListModel.h"
#import "NYTopicDetailModel.h"
#import "NYArticleDetailModel.h"
#import "NYMainModel.h"
#import "NYCommentListModel.h"
#import "NYMiniAdModel.h"



@implementation NYRecommendManager


+ (void)getCategoryListWithCompletionHandle:(void (^)(NYTopicCategoryModel *model, NSError *error))complete{
    [self GET:[NSString stringWithFormat:@"%@%@",Api_URL,kGetRecommendTopic]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
        NYTopicCategoryModel *model = [NYTopicCategoryModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}


+ (void)getRecommedSlideWithCompletionHandle:(void (^)(NYRecommendSlideModel *model, NSError *error))complete{
    [self GET:[NSString stringWithFormat:@"%@%@",Api_URL,kGetRecommendSlide]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
        NYRecommendSlideModel *model = [NYRecommendSlideModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}



+ (void)getMiniAdWithCompletionHandle:(void (^)(NYMiniAdModel *model, NSError *error))complete{
    [self GET:[NSString stringWithFormat:@"%@%@",Api_URL,kGetRecommendMiniAd]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
        NYMiniAdModel *model = [NYMiniAdModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}


//旧接口  改为不用id
//+ (void)getRecommedListWithPage:(NSString *)page andTopicID:(NSString *)ID andCompletionHandle:(void (^)(NYArticleListModel *model, NSError *error))complete{
//    [self GET:[NSString stringWithFormat:@"%@%@?p=%@&talkid=%@",Api_URL,kGetRecommendList,page,ID]
//   parameters:nil
//completionHandler:^(id responseObj, NSError *error) {
//        NYArticleListModel *model = [NYArticleListModel mj_objectWithKeyValues:responseObj];
//        if (model.code != 0) {
//            error = [self createError:model.msg];
//        }
//        complete(model,error);
//    }];
//}



+ (void)getRecommedListWithPage:(NSString *)page andCompletionHandle:(void (^)(NYArticleListModel *model, NSError *error))complete{
    [self GET:[NSString stringWithFormat:@"%@%@?p=%@",Api_URL,kGetRecommendList,page]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
        NYArticleListModel *model = [NYArticleListModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}

+ (void)getArticleWithArticleID:(NSString *)articleID andCompletionHandle:(void (^)(NYArticleDetailModel *model, NSError *error))complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:articleID forKey:@"articleid"];
    [dict setValue:uid forKey:@"userid"];
    [dict setValue:sessionid forKey:@"sessionid"];
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kNewsDetail]
    parameters:dict
completionHandler:^(id responseObj, NSError *error) {
        NYArticleDetailModel *model = [NYArticleDetailModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}

+ (void)getArticleCommentsWithArticleID:(NSString *)articleID andPage:(NSString *)page andCompletionHandle:(void (^)(NYCommentListModel *model, NSError *error))complete{
    [self GET:[NSString stringWithFormat:@"%@%@?articleid=%@&p=%@",Api_URL,kGetArticleComments,articleID,page]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
        NYCommentListModel *model = [NYCommentListModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}
+ (void)replyCommentWithArticleID:(NSString *)articleID andCommentID:(NSString *)commentid andContent:(NSString *)content andUserID:(NSString *)uid andSessionID:(NSString *)sessionid andCompletionHandle:(void (^)(NYMainModel *model, NSError *error))complete{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:uid forKey:@"userid"];
    [dict setValue:sessionid forKey:@"sessionid"];
    [dict setValue:commentid forKey:@"commentid"];
    [dict setValue:articleID forKey:@"articleid"];
    [dict setValue:content forKey:@"content"];
    
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kReplyArticleComment]
    parameters:dict
completionHandler:^(id responseObj, NSError *error) {
        NYMainModel *model = [NYMainModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}


+ (void)postCommentWithArticleID:(NSString *)articleID andTitle:(NSString *)title andContent:(NSString *)content andUserID:(NSString *)uid andSessionID:(NSString *)sessionid andCompletionHandle:(void (^)(NYMainModel *model, NSError *error))complete{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:uid forKey:@"userid"];
    [dict setValue:sessionid forKey:@"sessionid"];
    [dict setValue:title forKey:@"title"];
    [dict setValue:articleID forKey:@"articleid"];
    [dict setValue:content forKey:@"content"];
    
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kPostArticleComment]
    parameters:dict
completionHandler:^(id responseObj, NSError *error) {
        NYMainModel *model = [NYMainModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}
/*
 userid 用户id
 
 type 收藏类型 1文章 2话题
 
 dataid 收藏的数据id （type为1时，值为文章id；type为2时，值为话题id）
 
 sessionid 用户登录session串
 */

+(void)collectInfoWithUserID:(NSString *)uid andSessionID:(NSString *)sessionid andType:(NSString *)type andID:(NSString *)dataid andCompletionHandle:(void (^)(NYMainModel *, NSError *))complete{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:uid forKey:@"userid"];
    [dict setValue:sessionid forKey:@"sessionid"];
    [dict setValue:type forKey:@"type"];
    [dict setValue:dataid forKey:@"dataid"];
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kCollectArticle]
    parameters:dict
completionHandler:^(id responseObj, NSError *error) {
        NYMainModel *model = [NYMainModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
    
}

+(void)cancelCollectInfoWithUserID:(NSString *)uid andSessionID:(NSString *)sessionid andType:(NSString *)type andID:(NSString *)dataid andCompletionHandle:(void (^)(NYMainModel *, NSError *))complete{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:uid forKey:@"userid"];
    [dict setValue:sessionid forKey:@"sessionid"];
    [dict setValue:type forKey:@"type"];
    [dict setValue:dataid forKey:@"dataid"];
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kCancelCollectArticle]
    parameters:dict
completionHandler:^(id responseObj, NSError *error) {
        NYMainModel *model = [NYMainModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
    
}

+(void)likeInfoWithUserID:(NSString *)uid andSessionID:(NSString *)sessionid andID:(NSString *)dataid andCompletionHandle:(void (^)(NYMainModel *, NSError *))complete{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:uid forKey:@"userid"];
    [dict setValue:sessionid forKey:@"sessionid"];
    [dict setValue:dataid forKey:@"articleid"];
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kLikeArticle]
    parameters:dict
completionHandler:^(id responseObj, NSError *error) {
        NYMainModel *model = [NYMainModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
    
}


+(void)cancelLikeInfoWithUserID:(NSString *)uid andSessionID:(NSString *)sessionid andID:(NSString *)dataid andCompletionHandle:(void (^)(NYMainModel *, NSError *))complete{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:uid forKey:@"userid"];
    [dict setValue:sessionid forKey:@"sessionid"];
    [dict setValue:dataid forKey:@"articleid"];
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kCancelLikeArticle]
    parameters:dict
completionHandler:^(id responseObj, NSError *error) {
        NYMainModel *model = [NYMainModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
    
}
@end
